package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.AddbandcardReq;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_post;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;


/**
 * Created by Administrator on 2017/1/12.
 * 添加银行卡
 */

public class AddBankcardActivity extends BaseActivity implements View.OnClickListener {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private ImageView img_back;
    private EditText et_add_card_name, et_add_card_num, et_add_card_id, et_add_card_phone;
    private TextView tv_add_card_confirm, address;
    private RelativeLayout rly_address;
    private static final int REQUEST_CAMERA_CODE = 11;
    private static final int REQUEST_PREVIEW_CODE = 22;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addbankcardlayout);
        mActivity = this;

        init();
    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        et_add_card_name = (EditText) mActivity.findViewById(R.id.et_add_card_name);
        et_add_card_num = (EditText) mActivity.findViewById(R.id.et_add_card_num);
        et_add_card_id = (EditText) mActivity.findViewById(R.id.et_add_card_id);
        et_add_card_phone = (EditText) mActivity.findViewById(R.id.et_add_card_phone);
        rly_address = (RelativeLayout) mActivity.findViewById(R.id.rly_address);
        tv_add_card_confirm = (TextView) mActivity.findViewById(R.id.tv_add_card_confirm);
        address = (TextView) mActivity.findViewById(R.id.address);

        tv_add_card_confirm.setOnClickListener(this);
        rly_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add_card_confirm://确定
                if (et_add_card_name.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请填写名称");
                    return;
                }
                if (et_add_card_id.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请填写身份证");

                    return;
                }
                if (et_add_card_phone.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请填写手机号");

                    return;
                }
                if (et_add_card_num.getText().toString().equals("")) {
                    CommonUtil.showToast(mActivity, "请填写银行卡号");

                    return;
                }
                if (address.getText().toString().length() != 4) {
                    return;
                }

                Addbandcard();
                break;
            case R.id.rly_address://选择开户行
                Intent intent = new Intent(mActivity, BankcarActivity.class);
                intent.putExtra("code", 1);
                startActivityForResult(intent, 1);
                break;

        }
    }

    private void Addbandcard() {
        CustomProgressDialog.createDialog(mActivity).show();
        AddbandcardReq req = new AddbandcardReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        req.setAccount(et_add_card_num.getText().toString());
        req.setBank(address.getText().toString());
        req.setIdentifier_no(et_add_card_id.getText().toString());
        req.setMobile(et_add_card_phone.getText().toString());
        req.setName(et_add_card_name.getText().toString());

        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", req.getSecret());
        Async_http_post.post(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);

                    finish();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                address.setText(data.getExtras().getString("name"));
            }
        }
    }
}
